import * as Koa from 'koa';
import { addAnimation, delAnimation, updateAnimation, searchAnimation } from "../entity/animation";
import { joinDefaultFields } from "../utils/index";
import dayjs = require("dayjs");
// TODO：commonjs 模式下引用 esmodule 模块
let nanoid: any = null;
(async () => {
  nanoid = (await eval("import('nanoid')")).nanoid;
})();

const errorMsg = `服务器错误：${'animation'}`;
const successMsg = `成功：${'animation'}`;

export default class AnimationService {
  /**
   * 添加
   * @param ctx 
   */
  addAnimation = async (ctx: Koa.Context) => {
    try {
      const animation = ctx.request.body;
      const _animation = joinDefaultFields(animation, "animationFields");
      _animation.aid = nanoid(18);
      _animation.create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
      const result: any = _animation && await addAnimation(ctx, _animation);
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

    /**
   * 删除
   * @param ctx 
   */
  delAnimation = async (ctx: Koa.Context) => {
    try {
      const { aid } = ctx.request.body;
      if (!aid || !aid.length) return ctx.fail(null, "aid不能为空", -1);
      const aids = Array.isArray(aid) ? aid : [aid];
      let condition = `aid IN (${aids.map((i) => `"${i}"`).join(",")})`;
      const result: any =  !!aids.length && await delAnimation(ctx, { condition });
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

    /**
   * 修改
   * @param ctx 
   */
  updateAnimation = async (ctx: Koa.Context) => {
    try {
      const animation = ctx.request.body;
      animation.update_time = dayjs().format("YYYY-MM-DD HH:mm:ss"); // 最后的更新时间
      const result: any = animation && await updateAnimation(ctx, animation);
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

  /**
   * 查询
   * @param ctx 
   */
  searchAnimation = async (ctx: Koa.Context) => {
    try {
      const params = ctx.request.body;
      const { aid, title } = params || {}; // 查询条件
      // 相等的条件
      let condition = "";
      let equals: string | string[] = [];
      aid && (equals.push(` aid = "${aid}"`));
      title && (equals.push(` title LIKE "%${title}%"`));
      equals = equals.join(" AND");
      equals && (condition += `WHERE ${equals}`);
      const result: any = await searchAnimation(ctx, { condition });
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(result, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };
}
